﻿/*
--------------------------------------
File: GrowingSelection.cs
Project: Themaopdracht 7
Author: Maarten Kroon

Description:
Creates blobs based on the pixel color, only works with monochrome images
--------------------------------------
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace THO7_Team8
{
    public class GrowingSelection
    {
        int[] array;
        int[] originalArray;
        int[,] twoDimensionalArray;

        int width;
        int height;
        List<blob> blobs;
        int blobSize = 0;

        //A struct for a blob with a label(color) and size
        public struct blob
        {
            public int label, size;

            public blob(int nwLabel, int nwSize)
            {
                label = nwLabel;
                size = nwSize;
            }
        }

        //Constructor, copy's the inputArray into blob
        public GrowingSelection(int[] inputArray, int imageWidth, int imageHeight)
        {
            blobs = new List<blob>();
            array = new int[inputArray.Length];
            originalArray = new int[inputArray.Length];
            twoDimensionalArray = new int[imageHeight, imageWidth];

            inputArray.CopyTo(array, 0);
            inputArray.CopyTo(originalArray, 0);

            width = imageWidth;
            height = imageHeight; 
        }

        //Gets all the blobs of blobColor in the array, still uses recursive blob detection
        public void findBlobs(int blobColor)
        {
            int blobCounter = 0;
            for (int i = 0; i < array.Length; i++)
            {
                if (array[i] == blobColor)
                {
                    blobCounter++;
                    blobSize = 0;

                    createBlob(i, blobCounter, blobColor);
                    blob b = new blob(blobCounter, blobSize);
                    blobs.Add(b);
                }
            }
        }

        //Gives the blobs a new color
        public void colorBlobs(int nwColor)
        {
            foreach (blob b in blobs)
            {
                for (int i = 0; i < array.Length; i++)
                {
                    if (array[i] == b.label)
                    {
                        array[i] = nwColor;
                    }
                }
            }
        }               

        //Filters out blobs which are to small
        public void filterSmallBlobs(int minSize)
        {
            foreach (blob b in blobs)
            {
                if (b.size < minSize)
                {
                    for (int i = 0; i < array.Length; i++)
                    {
                        if (array[i] == b.label)
                        {
                            array[i] = -16777216;
                        }
                    }
                }
            }
        }

        public int[] getResult()
        {
            return array;
        }

        //*** The code bellow is still in development, commented out for now ***//

            //public void checkPixel(int pixel)
            //{
            //    if();

            //}     

            //Non recursivise blob detect

            //public void createBlobs(int blobColor)
            //{
            //    //The label for the blob
            //    int label = 0;

            //    //Loops through the array pixel per pixel and row per row
            //    for (int y = 0; y < height; y++)
            //    {
            //        for (int x = 0; x < width; x++)
            //        {
            //            //If a pixel is found with the blobColor
            //            if (array[y * width + x] == blobColor)
            //            {
            //                //Labels the pixel
            //                array[y * width + x] = label;
        
        //*** End of development code *** //

        //Creates a blob recursively, stack overflow may occur
        public void createBlob(int position, int label, int blobColor)
        {
            //Checks if the position is in the array
            if (position < array.Length && position >= 0)
            {
                //If the current pixel in the array is white
                if (array[position] == blobColor)
                {
                    blobSize++;
                    //Labels the blob by changing its color
                    array[position] = label;

                    //Checks the neighbour pixels of the current pixel
                    createBlob(position - 1, label, blobColor);
                    createBlob(position + 1, label, blobColor);
                    createBlob(position - width, label, blobColor);
                    createBlob(position + width, label, blobColor);
                }
            }            
        }
    }
}